#include "settinggriddialog.h"
#include "ui_settinggriddialog.h"
#include "colorbutton.h"

SettingGridDialog::SettingGridDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingGridDialog)
{
    ui->setupUi(this);
    _plotter = 0;

    colorMajorGridBtn = new ColorButton;
    colorMinorGridBtn = new ColorButton;

    ui->horizontalLayout_3->addWidget(colorMajorGridBtn);
    ui->horizontalLayout_6->addWidget(colorMinorGridBtn);

    ui->penStyleMajorGrid->addItem("SolidLine", QVariant(Qt::SolidLine));
    ui->penStyleMajorGrid->addItem("DashLine", QVariant(Qt::DashLine));
    ui->penStyleMajorGrid->addItem("DotLine", QVariant(Qt::DotLine));
    ui->penStyleMajorGrid->addItem("DashDotLine", QVariant(Qt::DashDotLine));
    ui->penStyleMajorGrid->addItem("DashDotDotLine", QVariant(Qt::DashDotDotLine));
    ui->penStyleMajorGrid->addItem("NoLine", QVariant(Qt::NoPen));

    ui->penStyleMinorGrid->addItem("SolidLine", QVariant(Qt::SolidLine));
    ui->penStyleMinorGrid->addItem("DashLine", QVariant(Qt::DashLine));
    ui->penStyleMinorGrid->addItem("DotLine", QVariant(Qt::DotLine));
    ui->penStyleMinorGrid->addItem("DashDotLine", QVariant(Qt::DashDotLine));
    ui->penStyleMinorGrid->addItem("DashDotDotLine", QVariant(Qt::DashDotDotLine));
    ui->penStyleMinorGrid->addItem("NoLine", QVariant(Qt::NoPen));

    QListWidgetItem *item_x = new QListWidgetItem(ui->listWidget);
    item_x->setIcon(QIcon(":/img/axisx.png"));
    item_x->setText(tr("Axis X"));
    item_x->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

    QListWidgetItem *item_y = new QListWidgetItem(ui->listWidget);
    item_y->setIcon(QIcon(":/img/axisy.png"));
    item_y->setText(tr("Axis Y"));
    item_y->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

    ui->listWidget->setFlow(QListView::TopToBottom);
    ui->listWidget->setCurrentRow(0);

    connect(ui->listWidget, SIGNAL(currentRowChanged(int)), this, SLOT(changeAxis(int)));
    connect(ui->penStyleMajorGrid, SIGNAL(currentIndexChanged(int)),
            this, SLOT(changePenStyleMajorGrid(int)));
    connect(ui->penStyleMinorGrid, SIGNAL(currentIndexChanged(int)),
            this, SLOT(changePenStyleMinorGrid(int)));
    connect(ui->penWidthMajorGrid, SIGNAL(valueChanged(int)),
            this, SLOT(changePenWidthMajorGrid(int)));
    connect(ui->penWidthMinorGrid, SIGNAL(valueChanged(int)),
            this, SLOT(changePenWidthMinorGrid(int)));
    connect(colorMajorGridBtn, SIGNAL(colorChanged(QColor)),
            this, SLOT(changeColorMajorGrid(QColor)));
    connect(colorMinorGridBtn, SIGNAL(colorChanged(QColor)),
            this, SLOT(changeColorMinorGrid(QColor)));
    connect(ui->showMajorGridCheckBox, SIGNAL(clicked(bool)),
            this, SLOT(changeShowMajorGrid(bool)));
    connect(ui->showMinorGridCheckBox, SIGNAL(clicked(bool)),
            this, SLOT(changeShowMinorGrid(bool)));
    connect(ui->applyBtn, SIGNAL(clicked()), this, SLOT(apply()));

}

SettingGridDialog::~SettingGridDialog()
{
    delete ui;
}

void SettingGridDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void SettingGridDialog::setPlotter(AbstractPlot2D *plotter)
{
    if (plotter) {
        _plotter = plotter;

        gridX = _plotter->gridX();
        gridY = _plotter->gridY();
        changeAxis(0);
    }
}

void SettingGridDialog::changeAxis(int currentRow)
{
    if (currentRow == 0) {
        int data = (int)gridX.penOfMajorGrid().style();
        int index = ui->penStyleMajorGrid->findData(QVariant(data));
        ui->penStyleMajorGrid->setCurrentIndex(index);

        data = (int)gridX.penOfMinorGrid().style();
        index = ui->penStyleMinorGrid->findData(QVariant(data));
        ui->penStyleMinorGrid->setCurrentIndex(index);

        ui->penWidthMajorGrid->setValue(gridX.penOfMajorGrid().width());
        ui->penWidthMinorGrid->setValue(gridX.penOfMinorGrid().width());

        colorMajorGridBtn->setColor(gridX.penOfMajorGrid().color());
        colorMinorGridBtn->setColor(gridX.penOfMinorGrid().color());

        ui->showMajorGridCheckBox->setChecked(gridX.showMajorGrid());
        ui->showMinorGridCheckBox->setChecked(gridX.showMinorGrid());
    } else
        if (currentRow == 1) {
        int data = (int)gridY.penOfMajorGrid().style();
        int index = ui->penStyleMajorGrid->findData(QVariant(data));
        ui->penStyleMajorGrid->setCurrentIndex(index);

        data = (int)gridY.penOfMinorGrid().style();
        index = ui->penStyleMinorGrid->findData(QVariant(data));
        ui->penStyleMinorGrid->setCurrentIndex(index);

        ui->penWidthMajorGrid->setValue(gridY.penOfMajorGrid().width());
        ui->penWidthMinorGrid->setValue(gridY.penOfMinorGrid().width());

        colorMajorGridBtn->setColor(gridY.penOfMajorGrid().color());
        colorMinorGridBtn->setColor(gridY.penOfMinorGrid().color());

        ui->showMajorGridCheckBox->setChecked(gridY.showMajorGrid());
        ui->showMinorGridCheckBox->setChecked(gridY.showMinorGrid());
    }

}

void SettingGridDialog::changePenStyleMajorGrid(int index)
{
    if (ui->listWidget->currentRow() == 0) {
        QPen pen = gridX.penOfMajorGrid();
        int penStyle = ui->penStyleMajorGrid->itemData(index).toInt();
        pen.setStyle((Qt::PenStyle)penStyle);
        gridX.setPenOfMajorGrid(pen);
    } else
        if (ui->listWidget->currentRow() == 1) {
        QPen pen = gridY.penOfMajorGrid();
        int penStyle = ui->penStyleMajorGrid->itemData(index).toInt();
        pen.setStyle((Qt::PenStyle)penStyle);
        gridY.setPenOfMajorGrid(pen);
    }
}

void SettingGridDialog::changePenStyleMinorGrid(int index)
{
    if (ui->listWidget->currentRow() == 0) {
        QPen pen = gridX.penOfMinorGrid();
        int penStyle = ui->penStyleMinorGrid->itemData(index).toInt();
        pen.setStyle((Qt::PenStyle)penStyle);
        gridX.setPenOfMinorGrid(pen);
    } else
        if (ui->listWidget->currentRow() == 1) {
        QPen pen = gridY.penOfMinorGrid();
        int penStyle = ui->penStyleMinorGrid->itemData(index).toInt();
        pen.setStyle((Qt::PenStyle)penStyle);
        gridY.setPenOfMinorGrid(pen);
    }
}

void SettingGridDialog::changePenWidthMajorGrid(int value)
{
    if (ui->listWidget->currentRow() == 0) {
        QPen pen = gridX.penOfMajorGrid();
        pen.setWidth(value);
        gridX.setPenOfMajorGrid(pen);
    } else
        if (ui->listWidget->currentRow() == 1) {
        QPen pen = gridY.penOfMajorGrid();
        pen.setWidth(value);
        gridY.setPenOfMajorGrid(pen);
    }
}

void SettingGridDialog::changePenWidthMinorGrid(int value)
{
    if (ui->listWidget->currentRow() == 0) {
        QPen pen = gridX.penOfMinorGrid();
        pen.setWidth(value);
        gridX.setPenOfMinorGrid(pen);
    } else
        if (ui->listWidget->currentRow() == 1) {
        QPen pen = gridY.penOfMinorGrid();
        pen.setWidth(value);
        gridY.setPenOfMinorGrid(pen);
    }
}

void SettingGridDialog::changeColorMajorGrid(const QColor &color)
{
    if (ui->listWidget->currentRow() == 0) {
        QPen pen = gridX.penOfMajorGrid();
        pen.setColor(color);
        gridX.setPenOfMajorGrid(pen);

    } else if (ui->listWidget->currentRow() == 1) {
        QPen pen = gridY.penOfMajorGrid();
        pen.setColor(color);
        gridY.setPenOfMajorGrid(pen);

    }
}

void SettingGridDialog::changeColorMinorGrid(const QColor &color)
{
    if (ui->listWidget->currentRow() == 0) {
        QPen pen = gridX.penOfMinorGrid();
        pen.setColor(color);
        gridX.setPenOfMinorGrid(pen);

    } else if (ui->listWidget->currentRow() == 1) {
        QPen pen = gridY.penOfMinorGrid();
        pen.setColor(color);
        gridY.setPenOfMinorGrid(pen);
    }
}

void SettingGridDialog::changeShowMajorGrid(bool isShow)
{
    if (ui->listWidget->currentRow() == 0) {
        gridX.showMajorGrid(isShow);
    } else if (ui->listWidget->currentRow() == 1) {
        gridY.showMajorGrid(isShow);
    }
}

void SettingGridDialog::changeShowMinorGrid(bool isShow)
{
    if (ui->listWidget->currentRow() == 0) {
        gridX.showMinorGrid(isShow);
    } else if (ui->listWidget->currentRow() == 1) {
        gridY.showMinorGrid(isShow);
    }
}

void SettingGridDialog::apply()
{
    if (_plotter) {
        _plotter->setGridX(gridX);
        _plotter->setGridY(gridY);
    }
}

